using System;
using Windows.ApplicationModel;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
using Log = Xiaowei.Diagnostics.Debug;
using Debug = Xiaowei.Diagnostics.Debug;
using Xiaowei.Services;

namespace Xiaowei.Settings
{
    public static class LocalSettings
    {
        public static string Guid { get; set; }
        public static string License { get; set; }
        public static string AppId { get; set; }
        public static int ProductId { get; set; }
        public static int KeyVersion { get; set; }
        //public static string PublicKey { get; set; }

        static Rect? _workingArea;
        public static Rect WorkingArea
        {
            get
            {
                if (!_workingArea.HasValue)
                {
                    _workingArea = (Rect)ReadValue("WorkingArea", new Rect(0, 0, 1920, 1040));
                }
                return _workingArea.Value;
            }
            set
            {
                if (_workingArea != value)
                {
                    _workingArea = value;
                    WriteValue("WorkingArea", value);
                }
            }
        }

        static bool? _wakeWordEnabled;
        public static bool WakeWordEnabled
        {
            get
            {
                if (!_wakeWordEnabled.HasValue)
                {
                    _wakeWordEnabled = (bool)ReadValue("WakeWordEnabled", false);
                }
                return _wakeWordEnabled.Value;
            }
            set
            {
                if (_wakeWordEnabled != value)
                {
                    _wakeWordEnabled = value;
                    WriteValue("WakeWordEnabled", value);
                }
            }
        }

        static bool? _AvailableUnderLockScreen;
        public static bool AvailabilityUnderLockScreen
        {
            get
            {
                if (!_AvailableUnderLockScreen.HasValue)
                {
                    _AvailableUnderLockScreen = (bool)ReadValue("AvailabilityUnderLockScreen", false);
                }
                return _AvailableUnderLockScreen.Value;
            }
            set
            {
                if (_AvailableUnderLockScreen != value)
                {
                    _AvailableUnderLockScreen = value;
                    WriteValue("AvailabilityUnderLockScreen", value);
                }
            }
        }



        static bool? _needReminderVoice;
        public static bool AckVoiceEnabled
        {
            get
            {
                if (!_needReminderVoice.HasValue)
                {
                    _needReminderVoice = (bool)ReadValue("ReminderVoice", true);
                }
                return _needReminderVoice.Value;
            }

            set
            {
                if (_needReminderVoice != value)
                {
                    _needReminderVoice = value;
                    WriteValue("ReminderVoice", value);
                }
            }
        }

        static bool? _autoStartup;
        public static bool AutoStartUp
        {
            get
            {
                if (!_autoStartup.HasValue)
                {
                    _autoStartup = (bool)ReadValue("AutoStartUp", false);
                }
                return _autoStartup.Value;
            }

            set
            {
                if (_autoStartup != value)
                {
                    _autoStartup = value;
                    WriteValue("AutoStartUp", value);
                }
            }
        }

        static bool? _startupDisabledByUser;
        public static bool StartUpDisabledByUser
        {
            get
            {
                if (!_startupDisabledByUser.HasValue)
                {
                    _startupDisabledByUser = (bool)ReadValue("StartUpDisabledByUser", false);
                }
                return _startupDisabledByUser.Value;
            }

            set
            {
                if (_startupDisabledByUser != value)
                {
                    _startupDisabledByUser = value;
                    WriteValue("StartUpDisabledByUser", value);
                }
            }
        }

        static bool? _hotKeyEnabled;
        public static bool HotKeyEnabled
        {
            get
            {
                if (!_hotKeyEnabled.HasValue)
                {
                    _hotKeyEnabled = (bool)ReadValue("HotKeyEnabled", false);
                }
                return _hotKeyEnabled.Value;
            }

            set
            {
                if (_hotKeyEnabled != value)
                {
                    _hotKeyEnabled = value;
                    WriteValue("HotKeyEnabled", value);
                }
            }
        }

        static string _hotKey;
        public static string HotKey
        {
            get
            {
                if (string.IsNullOrEmpty(_hotKey))
                {
                    _hotKey = (string)ReadValue("HotKey", "R");
                }
                return _hotKey;
            }
            set
            {
                if (_hotKey != value)
                {
                    _hotKey = value;
                    WriteValue("HotKey", _hotKey);
                    HotKeyChanged?.Invoke(value);
                }
            }
        }

        public static event Action<string> HotKeyChanged;

        static string _hotKeyPrefix;
        public static string HotKeyPrefix
        {
            get
            {
                if (string.IsNullOrEmpty(_hotKeyPrefix))
                {
                    _hotKeyPrefix = (string)ReadValue("HotKeyPrefix", "Ctrl + Shift + ");
                }
                return _hotKeyPrefix;
            }
            set
            {
                if (_hotKeyPrefix != value)
                {
                    _hotKeyPrefix = value;
                    WriteValue("HotKeyPrefix", _hotKeyPrefix);
                }
            }
        }

        static string _serialNumber;
        public static string SerialNumber
        {
            get
            {
                if (string.IsNullOrEmpty(_serialNumber))
                {
                    _serialNumber = (string)ReadValue("SerialNumber", "");
                }
                return _serialNumber;
            }
            set
            {
                if (_serialNumber != value)
                {
                    _serialNumber = value;

                    WriteValue("SerialNumber", _serialNumber);
                }
            }
        }

        static string _ssid;
        public static string SSID
        {
            get
            {
                if (string.IsNullOrEmpty(_ssid))
                {
                    _ssid = (string)ReadValue("SSID", "");
                }
                return _ssid;
            }
            set
            {
                if (_ssid != value)
                {
                    _ssid = value;

                    WriteValue("SSID", _ssid);
                }
            }
        }
        static int? _qqMusicInstallState;
        public static int QQMusicInstallState
        {
            get
            {
                if (!_qqMusicInstallState.HasValue)
                {
                    _qqMusicInstallState = (int)ReadValue("QQMusicInstallState", 0);
                }
                return _qqMusicInstallState.Value;
            }
            set
            {
                if (_qqMusicInstallState != value)
                {
                    _qqMusicInstallState = value;

                    WriteValue("QQMusicInstallState", _qqMusicInstallState);
                }
            }
        }
        static bool? _firstQQMusicCommand;
        public static bool FirstQQMusicCommand
        {
            get
            {
                if (!_firstQQMusicCommand.HasValue)
                {
                    _firstQQMusicCommand = (bool)ReadValue("FirstQQMusicCommand", true);
                }
                return _firstQQMusicCommand.Value;
            }
            set
            {
                if (_firstQQMusicCommand != value)
                {
                    _firstQQMusicCommand = value;

                    WriteValue("FirstQQMusicCommand", _firstQQMusicCommand);
                }
            }
        }

        static int? _tencentVedioInstallState;
        public static int TencentVedioInstallState
        {
            get
            {
                if (!_tencentVedioInstallState.HasValue)
                {
                    _tencentVedioInstallState = (int)ReadValue("TencentVedioInstallState", 0);
                }
                return _tencentVedioInstallState.Value;
            }
            set
            {
                if (_tencentVedioInstallState != value)
                {
                    _tencentVedioInstallState = value;

                    WriteValue("TencentVedioInstallState", _tencentVedioInstallState);
                }
            }
        }

        static string _deviceName;
        public static string DeviceName
        {
            get
            {
                if (string.IsNullOrEmpty(_deviceName))
                {
                    _deviceName = (string)ReadValue("DeviceName", "");
                }
                return _deviceName;
            }
            set
            {
                if (_deviceName != value)
                {
                    _deviceName = value;

                    WriteValue("DeviceName", _deviceName);
                }
            }
        }
        static string _productNumber;
        public static string ProductNumber
        {
            get
            {
                if (string.IsNullOrEmpty(_productNumber))
                {
                    _productNumber = (string)ReadValue("ProductNumber", "");
                }
                return _productNumber;
            }
            set
            {
                if (_productNumber != value)
                {
                    _productNumber = value;

                    WriteValue("ProductNumber", _productNumber);
                }
            }
        }

        static string _customerId;
        public static string CustomerId
        {
            get
            {
                return _customerId;
            }
            set
            {
                if (_customerId != value)
                {
                    _customerId = value;
                }
            }
        }

        public static event Action WelcomePageEnabledChanged;
        static bool? _welcomePageEnabled;
        public static bool WelcomePageEnabled
        {
            get
            {
                if (!_welcomePageEnabled.HasValue)
                {
                    _welcomePageEnabled = (bool)ReadValue("WelcomePageEnabled", true);
                }
                return _welcomePageEnabled.Value;
            }
            set
            {
                if (_welcomePageEnabled != value)
                {
                    _welcomePageEnabled = value;
                    WriteValue("WelcomePageEnabled", _welcomePageEnabled);
                    WelcomePageEnabledChanged?.Invoke();
                }
            }
        }

        static bool? _logEnabled;
        public static bool LogEnabled
        {
            get
            {
                if (!_logEnabled.HasValue)
                {
                    _logEnabled = (bool)ReadValue("LogEnabled", false);
                }
                return _logEnabled.Value;
            }
            set
            {
                if (_logEnabled != value)
                {
                    _logEnabled = value;

                    WriteValue("LogEnabled", _logEnabled);
                }
            }
        }

        public static string HostAddress => DebugConfig.HostAddress;

        public static string Eula
        {
            get { return HostAddress + "/eula"; }
        }
        public static string UC
        {
            get { return HostAddress + "/uc"; }
        }
        public static string Privacy
        {
            get
            {
                return "https://www8.hp.com/cn/zh/privacy/privacy.html ";
            }
        }
        public static string SystemVersion { get; set; }
        public static Version LatestVersion
        {
            get;
            set;
        }
        
        //public static readonly string KwsHost = "https://xiaowei.weixin.qq.com";
        //public static readonly string KwsTokenUrl = "/xwcloudapi/auth_get_token";
        //public static readonly string KwsAsrUrl = "/xwcloudapi/asr";
        //public static readonly string KwsTokenSecretKey = "ySWaXQfckZEHlBxOIuM3zxKid41P5VubUWUQneiWBxms1Twd";
        //public static readonly int KwsTokenPid = 393;


        static void WriteValue(string key, object value)
        {
            ApplicationData.Current.LocalSettings.Values[key] = value;
        }

        static object ReadValue(string key, object defaultValue)
        {
            return ApplicationData.Current.LocalSettings.Values[key] ?? defaultValue;
        }
    }
}
